﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.Services;
using Jn698._45GWTool.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels.Controls
{
    public class AddOrUpdateConnectPlanViewModel:ViewModelBase
    {
        public static GWCmdMakeUp GWCmdMakeUp;
        private GWCmdMakeUpService gWCmdMakeUpService = new GWCmdMakeUpService();
        private int totalNum;

        public int TotalNum
        {
            get { return totalNum; }
            set { totalNum = value; RaisePropertyChanged();  GetDataInfo(value); }
        }

        private List<int> totalNumList;

        public List<int> TotalNumList
        {
            get { return totalNumList; }
            set { totalNumList = value; RaisePropertyChanged(); }
        }


        private string number;

        public string Number
        {
            get { return number; }
            set { number = value; RaisePropertyChanged(); }
        }

        private string deep;

        public string Deep
        {
            get { return deep; }
            set { deep = value; RaisePropertyChanged(); }
        }

        private int typeSelected;

        public int TypeSelected
        {
            get { return typeSelected; }
            set { typeSelected = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> typeList;

        public ObservableCollection<string> TypeList
        {
            get { return typeList; }
            set { typeList = value; RaisePropertyChanged(); }
        }

        private string msData;

        public string MSData
        {
            get { return msData; }
            set { msData = value; RaisePropertyChanged(); }
        }

        private int saveTimeSelected;

        public int SaveTimeSelected
        {
            get { return saveTimeSelected; }
            set { saveTimeSelected = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> saveTimeList;

        public ObservableCollection<string> SaveTimeList
        {
            get { return saveTimeList; }
            set { saveTimeList = value; RaisePropertyChanged(); }
        }


        private string groupName;

        public string GroupName
        {
            get { return groupName; }
            set { groupName = value; RaisePropertyChanged(); }
        }

        private int selectedIndex;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; RaisePropertyChanged(); GroupName = $"第{value + 1}个配置单元"; UpDataInfo(); }
        }

        private string content;

        public string Content
        {
            get { return content; }
            set { content = value; RaisePropertyChanged(); }
        }


        private ObservableCollection<GWCmdMakeUp> gWCmdMakeUpList;

        public ObservableCollection<GWCmdMakeUp> GWCmdMakeUpList
        {
            get { return gWCmdMakeUpList; }
            set { gWCmdMakeUpList = value; RaisePropertyChanged(); }
        }

        private List<SingleData> roadList;

        public List<SingleData> RoadList
        {
            get { return roadList; }
            set { roadList = value; }
        }

        private ObservableCollection<RCSDDataModel> rcsdDataList;

        public ObservableCollection<RCSDDataModel> RcsdDataList
        {
            get { return rcsdDataList; }
            set { rcsdDataList = value; }
        }

        private List<string> unitList;

        public List<string> UnitList
        {
            get { return unitList; }
            set { unitList = value; RaisePropertyChanged(); }
        }
        private int unitSelected;

        public int UnitSelected
        {
            get { return unitSelected; }
            set { unitSelected = value; RaisePropertyChanged(); }
        }

        private string cycle;

        public string Cycle
        {
            get { return cycle; }
            set { cycle = value; RaisePropertyChanged(); }
        }

        public AddOrUpdateConnectPlanViewModel()
        {
            
            RcsdDataList = new ObservableCollection<RCSDDataModel>();
            RoadList = new List<SingleData>();
            RoadList.Add(new SingleData { Name = "对象属性描述符" });
            RoadList.Add(new SingleData { Name = "记录型对象属性描述符" });
            TotalNumList = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            TypeList = new ObservableCollection<string>() { "0-采集当前数据","1-采集上第n次","2-按时标采集","3-按时标间隔采集","4-补抄"};
            SaveTimeList = new ObservableCollection<string>() { "未定义","任务开始时间","相对当日0点0分","相对上日23点59分","相对上日0点0分","相对当月1日0点0分","数据冻结时标","相对上月月末0点0分"};
            UnitList = new List<string>() { "秒","分","时","日","月","年"};
        }

        public void GetDataInfo(int total)
        {
            GWCmdMakeUpList = new ObservableCollection<GWCmdMakeUp>(gWCmdMakeUpService.SelectByOAD("60147F00", total));
            if (GWCmdMakeUpList.Count <= 0)
            {
                return;
            }
            SelectedIndex = 0;
            //GWCmdMakeUp = null;
            UpDataInfo();
        }
        public void UpDataInfo()
        {
            if (SelectedIndex < 0)
            {
                SelectedIndex = 0;
            }
            if (GWCmdMakeUpList.Count <= 0)
            {
                return;
            }
            //判断之前是否点击过，如果点击过则保存之前的数据
            if (GWCmdMakeUp != null)
            {
                SaveDataInfo();
            }

            GWCmdMakeUp = GWCmdMakeUpList[SelectedIndex];
            if (GWCmdMakeUp == null)
            {
                return;
            }
            try
            {
                RcsdDataList.Clear();
                var datas = GWCmdMakeUp.DataValue.Split(';');
                if (datas.Length < 5)
                {
                    return;
                }
                Number = datas[0];
                Deep = datas[1];
                TypeSelected = int.Parse(datas[2]);
                string[] adds;
                if (TypeSelected==1)
                {
                    Content = datas[3];
                    adds = datas[4]?.Split(',');
                    int num = adds.Length / 2;

                    for (int i = 0; i < num; i++)
                    {
                        RCSDDataModel rCSDDataModel = new RCSDDataModel();
                        if (int.Parse(adds[2 * i]) == 0)
                        {
                            rCSDDataModel.CSD = "对象属性描述符";
                        }
                        else
                        {
                            rCSDDataModel.CSD = "记录型对象属性描述符";
                        }
                        rCSDDataModel.Data = adds[2 * i + 1];
                        RcsdDataList.Add(rCSDDataModel);
                    }
                    MSData = datas[5];
                    SaveTimeSelected = int.Parse(datas[6]);
                }
                else if (TypeSelected==3)
                {
                    Content = datas[3];
                    UnitSelected = int.Parse(datas[4]);
                    adds = datas[5]?.Split(',');
                    int num = adds.Length / 2;

                    for (int i = 0; i < num; i++)
                    {
                        RCSDDataModel rCSDDataModel = new RCSDDataModel();
                        if (int.Parse(adds[2 * i]) == 0)
                        {
                            rCSDDataModel.CSD = "对象属性描述符";
                        }
                        else
                        {
                            rCSDDataModel.CSD = "记录型对象属性描述符";
                        }
                        rCSDDataModel.Data = adds[2 * i + 1];
                        RcsdDataList.Add(rCSDDataModel);
                    }
                    MSData = datas[6];
                    SaveTimeSelected = int.Parse(datas[7]);
                }
                else if (TypeSelected ==4)
                {
                    Content = datas[3];
                    UnitSelected = int.Parse(datas[4]);
                    Cycle = datas[5];
                    adds = datas[6]?.Split(',');
                    int num = adds.Length / 2;

                    for (int i = 0; i < num; i++)
                    {
                        RCSDDataModel rCSDDataModel = new RCSDDataModel();
                        if (int.Parse(adds[2 * i]) == 0)
                        {
                            rCSDDataModel.CSD = "对象属性描述符";
                        }
                        else
                        {
                            rCSDDataModel.CSD = "记录型对象属性描述符";
                        }
                        rCSDDataModel.Data = adds[2 * i + 1];
                        RcsdDataList.Add(rCSDDataModel);
                    }
                    MSData = datas[7];
                    SaveTimeSelected = int.Parse(datas[8]);
                }
                else
                {
                    adds = datas[3]?.Split(',');
                    int num = adds.Length / 2;

                    for (int i = 0; i < num; i++)
                    {
                        RCSDDataModel rCSDDataModel = new RCSDDataModel();
                        if (int.Parse(adds[2 * i]) == 0)
                        {
                            rCSDDataModel.CSD = "对象属性描述符";
                        }
                        else
                        {
                            rCSDDataModel.CSD = "记录型对象属性描述符";
                        }
                        rCSDDataModel.Data = adds[2 * i + 1];
                        RcsdDataList.Add(rCSDDataModel);
                    }
                    MSData = datas[4];
                    SaveTimeSelected = int.Parse(datas[5]);
                }
                
                
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }


        }

        public void SaveDataInfo()
        {
            if (GWCmdMakeUp == null)
            {
                return;
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(Number);
            stringBuilder.Append(";");
            stringBuilder.Append(Deep);
            stringBuilder.Append(";");
            stringBuilder.Append(TypeSelected.ToString());
            stringBuilder.Append(";");
            if (TypeSelected==1)
            {
                stringBuilder.Append(Content);
                stringBuilder.Append(";");
            }
            else if (TypeSelected == 3)
            {
                stringBuilder.Append(UnitSelected.ToString());
                stringBuilder.Append(";");
                stringBuilder.Append(Content);
                stringBuilder.Append(";");
                
            }
            else if (TypeSelected == 4)
            {
                stringBuilder.Append(UnitSelected.ToString());
                stringBuilder.Append(";");
                stringBuilder.Append(Content);
                stringBuilder.Append(";");
               
                stringBuilder.Append(Cycle);
                stringBuilder.Append(";");
            }
            for (int i = 0; i < RcsdDataList.Count; i++)
            {
                RCSDDataModel rCSDDataModel = RcsdDataList[i];
                if (rCSDDataModel.CSD.Equals("对象属性描述符"))
                {
                    stringBuilder.Append("00");
                }
                else
                {
                    stringBuilder.Append("01");
                }
                stringBuilder.Append(",");
                stringBuilder.Append(rCSDDataModel.Data);
                stringBuilder.Append(",");

            }
            stringBuilder.Append(";");
            stringBuilder.Append(MSData);
            stringBuilder.Append(";");
            stringBuilder.Append(SaveTimeSelected.ToString());
            stringBuilder.Append(";");


            GWCmdMakeUp.DataValue = stringBuilder.ToString();
            gWCmdMakeUpService.Update(GWCmdMakeUp);
        }

        public RelayCommand MSDataCommand
        {
            get
            {
                var command = new RelayCommand(() =>
                {
                    MSDataModel mSDataModel = new MSDataModel();
                    mSDataModel.Data = MSData;
                    SimpleIoc.Default.Unregister<MeterListViewModel>();
                    SimpleIoc.Default.Unregister<MSDataModel>();
                    GalaSoft.MvvmLight.Ioc.SimpleIoc.Default.Register<MeterListViewModel>();
                    GalaSoft.MvvmLight.Ioc.SimpleIoc.Default.Register(() => mSDataModel);
                    MeterListView meterListView = new MeterListView();
                    if (meterListView.ShowDialog()==true)
                    {
                        MSData = mSDataModel.Data;
                    }
                });
                return command;
            }
        }
    }
}
